/**
 * RFID Controller
 */

angular.module('NxtMap')
  .controller('RfidCtrl', ['$scope', '$rootScope', '$state', '$http', '$timeout', '$interval', 'MapService', RfidCtrl]);

function RfidCtrl($scope, $rootScope, $state, $http, $timeout, $interval, MapService) {
  $rootScope.loop = 1;
  MapService.flyHome();
  MapService.clearMap();
  $scope.isSelectorFold = false;
  $scope.toggleSelector = function toggle() {
    $scope.isSelectorFold = !$scope.isSelectorFold;
  }

  var allPoints = [];
  $.getJSON('data/data.json', function (data){
    allPoints = data.data;
  });

  $scope.searchId = '';
  $scope.selectCard = '3610442689';

  $scope.showTrack = function (card_id) {
    if($scope.selectCard != card_id){
      $scope.selectCard = card_id;
      
      draw();
    }
  }
  // 添加实体的点击操作
  var scene = MapService.viewer.scene;
  // handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  MapService.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
  MapService.handler.setInputAction(function(movement) {
    var pickedObject = scene.drillPick(movement.position);
    var checked = -1;
    if (Cesium.defined(pickedObject)) {
      for(var i = 0; i < pickedObject.length; i++){
        checked = pickedObject[i].id.name;
      }
    }
    if($scope.selectCard != checked && checked != -1 && checked) {
      $scope.selectCard = checked;
      
    }
    draw();
    $scope.$apply();
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

  $scope.moveEntityId = '';
  MapService.handler.setInputAction(function(movement) {
    var pickedObject = scene.pick(movement.endPosition);
    if($scope.moveEntityId) {
      var entity = MapService.viewer.entities.getById($scope.moveEntityId);
      if(entity) {
        entity.label.show = false;
      }
    }
    if (Cesium.defined(pickedObject)) {
      $scope.moveEntityId = pickedObject.id.id + '-label';
      var entity = MapService.viewer.entities.getById($scope.moveEntityId);
      if(entity) {
        entity.label.show = true;
      }
    }
    else {
      $scope.moveEntityId = '';
    }
    $scope.$apply();
  }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

  var url_origin = $rootScope.url_origin + '/rfid_cards';
  var i = 1;
  function loadNewData() {
    if($rootScope.loop == 0) {

      return;
    }
    var url_path = '?filter={"limit":' + i + '}';
    // $scope.originData = [];
    $http.get(url_origin + url_path).success(function (data) {
      // $scope.originData = data;
      $scope.tracks = generateTrackArray(data);
      draw();
      $scope.tparam = {
        total: $scope.originData.length,
        max: Math.ceil($scope.originData.length / 10),
        pages: [],
        now: 1
      }
      $scope.tparam.pages = generatePageNum($scope.tparam.now, $scope.tparam.max);
    });
    i++;
    $timeout(loadNewData, 2000);
  }
  $timeout(loadNewData, 2000);

  MapService.drawCrossTest();
  function draw() {
    MapService.clearMap();
    MapService.drawTrackPoint($scope.originData);
    // MapService.drawCrossTest();
    if($scope.selectCard.length > 0) {
      MapService.drawTrack($scope.tracks[$scope.selectCard]);
    }
  }

  $scope.goPage = function (value) {
    if(value == 'pre') {
      if($scope.tparam.now > 1) {
        $scope.tparam.now --;
      }
    }
    else if(value == 'next') {
      if($scope.tparam.now < $scope.tparam.max){
        $scope.tparam.now ++;
      }
    }
    else if(value == '···' || value == '··') {
      return;
    }
    else {
      $scope.tparam.now = value;
    }
    $scope.tparam.pages = generatePageNum($scope.tparam.now, $scope.tparam.max);
  }
  function generatePageNum(index, max) {
    var result = [];
    var num = 5;
    if(max < 8){
      var ii = 1;
      while (ii <= max) {
        result.push(ii);
        ii++;
      }
      return result;
    }
    var minv = index - 2, maxv = index + 2;
    if(minv <= 1) {
      maxv = 1 - minv + maxv;
      minv = 1;
    }
    else {
      result.push('··');
    }
    if(maxv >= max) {
      minv = minv - (maxv - max);
      maxv = max;     
    }
    for(var i = minv; i <= maxv; i++){
      result.push(i);
    }
    if(maxv < max) {
      if(maxv < max - 1){
        result.push('···');
      }
      result.push(max);
    }
    return result;
  }

  //轨迹信息测试用
  function generateTrackArray(data) {
    var tracks = {};
    $scope.originData = [];
    var map = {};
    var index = 0;
    for(var i = 0; i < data.length; i++) {
      if(!tracks[data[i].card_id]) {
        tracks[data[i].card_id] = [];
        map[data[i].card_id] = index;
        index ++;
      }
      tracks[data[i].card_id].push(data[i]);
      $scope.originData[map[data[i].card_id]] = data[i];
    }
    return tracks;
  }

}